package edu.unipd.p2t.core;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.ExpandableListActivity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import edu.unipd.p2t.R;
import edu.unipd.p2t.db.DataManager;
import edu.unipd.p2t.db.Message;

public class MessagesList extends ExpandableListActivity {

	 private static final String TAG = "MS";
	 private String authorList[][];
	 private String textList[]; 
	 private SimpleExpandableListAdapter expListAdapter=null;
	    @Override
	    public void onCreate(Bundle icicle)
	    {
	        super.onCreate(icicle);
	        
	        
	        //carico messaggi
            DataManager myDB=DataManager.getInstance(this);
    		
            Message[] m=myDB.getMessages();
            Log.i(TAG,"righe: "+Integer.toString(m.length));
            authorList=new String[m.length][2];
            textList=new String[m.length];
            for(int i=0;i<m.length;i=i+1)
            {
             	String author=m[i].getAuthor();
             	SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
             	authorList[i][0]=author;
             	authorList[i][1]=sdf.format(m[i].getDate());
             	textList[i]=m[i].getText();
             	Log.i(TAG,"dati: "+author);
            }
            
            //imposto la view
	        setContentView(R.layout.messageslist);
			expListAdapter =
				new SimpleExpandableListAdapter(
					this,
					createGroupList(),	// Lista dei dati primo livello 
					R.layout.messageslistgroup,	// Layout primo livello
					new String[] { "authorName", "dateMessage" },// Valori chiave del primo livello
					new int[] { R.id.childname, R.id.date },// Dati primo livello in riferimento ai valori chiave
					createChildList(),	// Lista dei dati secondo livello
					R.layout.messageslistrow,	// Layout secondo livello
					new String[] { "messageText" },	// Valori chiave del secondo livello
					new int[] { R.id.childname }	// Dati secondo livello in riferimento ai valori chiave
				);

			setListAdapter( expListAdapter );
			
			//cambio immagini di default
	        Drawable icon=this.getResources().getDrawable(R.drawable.expander_group);
    	    getExpandableListView().setGroupIndicator(icon);
	    }

	    public void  onContentChanged  () {
	        super.onContentChanged();
	        Log.d( TAG, "onContentChanged" );
	    }
	    

	    public boolean onChildClick(
	            ExpandableListView parent, 
	            View v, 
	            int groupPosition,
	            int childPosition,
	            long id) {
	        Log.d( TAG, "onChildClick: "+childPosition );
	        
	        return false;
	    }

	    public void  onGroupExpand  (int groupPosition) {
	        Log.d( TAG,"onGroupExpand: "+groupPosition );
	        /**Tentativo di far espandere un solo gruppo alla volta
	         * for(int i=0;i<expListAdapter.getGroupCount();i++)
	        	if(i!=groupPosition)
	        	{
	        		if(expListAdapter.getGroup(i) instanceof List)
	        			Log.i(TAG,"siiiiiiiiiiiiiii");
	        		else
	        			Log.i(TAG,"doh!");

	        	}
	        */
	        
	    }

	/**
	  * Metodo che ritorna la lista dei nomi degli autori dei messaggi 
	  */
		private List<HashMap<String,String>> createGroupList() {
		  ArrayList<HashMap<String,String>> result = new ArrayList<HashMap<String,String>>();
		  for( int i = 0 ; i < authorList.length ; ++i ) {
			HashMap<String,String> m = new HashMap<String,String>();
		    m.put( "authorName", authorList[i][0] );
		    m.put( "dateMessage", authorList[i][1] );
			result.add( m );
		  }
		  return (List<HashMap<String,String>>)result;
	    }

	/**
	  * Metodo che ritorna una lista di liste dei messaggi
	  */
	  private List<ArrayList<HashMap<String,String>>> createChildList() {
		ArrayList<ArrayList<HashMap<String,String>>> result = new ArrayList<ArrayList<HashMap<String,String>>>();
		ArrayList<HashMap<String,String>> second=null;
		for( int i = 0 ; i < textList.length ; ++i ) {
		  second=new ArrayList<HashMap<String,String>>();
		  HashMap<String,String> child = new HashMap<String,String>();
		  child.put( "messageText", textList[i] );
		  second.add( child );
		  result.add( second );
		}
		return result;
	  }
}
